# coding=utf-8
import time
import wx
from ConfigParser import ConfigParser
from AboutFrame import AboutFrame
from TechInputFrame import TechInputFrame
from TechFindFrame import TechFindFrame
from TechView import TechView
from TechDao import TechDao

class TechLogApp(wx.App):
    
    def __init__(self):
        wx.App.__init__(self,redirect=False)
    
    def OnInit(self):
        self.setting=ConfigParser()
        self.setting.read("setup/setting.ini")
        frmMain=MainFrame(self.setting)
        frmMain.Show()
        self.SetTopWindow(frmMain)
        return True
    
    def OnExit(self):
        pass

class MainFrame(wx.Frame):

    def __init__(self,setting):
        self.setting=setting
        self.pagesize=38
        self.page=1
        self.where=""
        
        wx.Frame.__init__(self,None,-1,u"记事本",style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX),size=(800,600))
        self.Center()
        #创建控件
        self.menuBar=wx.MenuBar()
        self.menuTech=wx.Menu()
        self.menuTechAdd=self.menuTech.Append(wx.NewId(),u"&A增加\tINS")
        self.menuTechEdit=self.menuTech.Append(wx.NewId(),u"&E修改\tF2")
        self.menuTechTextView=self.menuTech.Append(wx.NewId(),u"&T文本查看\tF3")
        self.menuTechHtmlView=self.menuTech.Append(wx.NewId(),u"&H网页查看\tF4")
        self.menuTechDel=self.menuTech.Append(wx.NewId(),u"&D删除\tDEL")
        self.menuTechRefresh=self.menuTech.Append(wx.NewId(),u"&R刷新\tF5")
        self.menuTechFind=self.menuTech.Append(wx.NewId(),u"&F查找\tCtrl+F")
        self.menuBar.Append(self.menuTech,u"&T记事")
        self.menuNav=wx.Menu()
        self.menuNavNext=self.menuNav.Append(wx.NewId(),u"&N下一页\tPGDN")
        self.menuNavPrev=self.menuNav.Append(wx.NewId(),u"&P上一页\tPGUP")
        self.menuBar.Append(self.menuNav,u"&G导航")
        self.menuHelp=wx.Menu()
        self.menuHelpAbout=self.menuHelp.Append(wx.NewId(),u"&A关于")
        self.menuBar.Append(self.menuHelp,u"&H帮助")
        self.SetMenuBar(self.menuBar)
        self.statusBar=self.CreateStatusBar(number=2)
        self.statusBar.SetStatusWidths([-1,140])       
        self.list=wx.ListCtrl(self,wx.NewId(),style=wx.LC_REPORT)
        self.list.InsertColumn(0,u"序号",width=60)
        self.list.InsertColumn(1,u"标题",width=120)
        self.list.InsertColumn(2,u"录入时间",width=140)
        self.list.InsertColumn(3,u"修改时间",width=140)
        self.list.InsertColumn(4,u"内容",width=300)
        #绑定事件
        self.Bind(wx.EVT_MENU,self.OnTechAdd,self.menuTechAdd)
        self.Bind(wx.EVT_MENU,self.OnTechEdit,self.menuTechEdit)
        self.Bind(wx.EVT_MENU,self.OnTechTextView,self.menuTechTextView)
        self.Bind(wx.EVT_MENU,self.OnTechHtmlView,self.menuTechHtmlView)
        self.Bind(wx.EVT_MENU,self.OnTechDel,self.menuTechDel)
        self.Bind(wx.EVT_MENU,self.OnTechRefresh,self.menuTechRefresh)
        self.Bind(wx.EVT_MENU,self.OnTechFind,self.menuTechFind)
        self.Bind(wx.EVT_MENU,self.OnNavNext,self.menuNavNext)
        self.Bind(wx.EVT_MENU,self.OnNavPrev,self.menuNavPrev)
        self.Bind(wx.EVT_CLOSE,self.OnDestroy)     
        self.Bind(wx.EVT_MENU,self.OnHelpAbout,self.menuHelpAbout)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnTechTextView, self.list)
        #provider = wx.CreateFileTipProvider("tips.txt", 0)
        #wx.ShowTip(None, provider, True)
        self.LoadData()
    
    def LoadData(self):
        techdao=TechDao()
        self.pagecount=techdao.pagecount(self.pagesize,self.where)
        rows=techdao.selectpage(self.pagesize,self.page,self.where)
        self.list.DeleteAllItems()
        for row in rows:
            item=self.list.InsertStringItem(self.list.GetItemCount(),str(row[0]))
            self.list.SetStringItem(item,1,row[1].encode('gbk','ignore'))
            self.list.SetStringItem(item,2,str(row[2]))
            self.list.SetStringItem(item,3,str(row[3]))
            self.list.SetStringItem(item,4,row[4].encode('gbk','ignore'))
        self.statusBar.SetStatusText("%d/%d"%(self.page,self.pagecount),1)
   
    def OnTechAdd(self,event):
        id=0
        self.frmTechInput=TechInputFrame(self,"add",id)
        self.frmTechInput.Show(True)
    
    def OnTechEdit(self,event):
        if self.list.GetFirstSelected()!=-1:
            id=self.list.GetItemText(self.list.GetFirstSelected())
            self.frmTechInput=TechInputFrame(self,"edit",id)
            self.frmTechInput.Show(True)
           
    
    def OnTechTextView(self,event):
        if self.list.GetFirstSelected()!=-1:
            id=self.list.GetItemText(self.list.GetFirstSelected())            
            self.frmTechView=TechView(self,'text',id)
            self.frmTechView.Show(True)
    
    def OnTechHtmlView(self,event):
        if self.list.GetFirstSelected()!=-1:
            id=self.list.GetItemText(self.list.GetFirstSelected())            
            self.frmTechView=TechView(self,'html',id)
            self.frmTechView.Show(True)
  
    def OnTechDel(self,event):
        if self.list.GetFirstSelected()!=-1:
            d=wx.MessageBox(u"是否要删除？",u"提示", wx.YES_NO | wx.ICON_QUESTION, self)
            if d==wx.YES:
                id=self.list.GetItemText(self.list.GetFirstSelected())
                techdao=TechDao()
                techdao.delete(id)
                self.LoadData()
    
    def OnTechRefresh(self,event):
        self.LoadData()
    
    def OnTechFind(self,event):
        self.frmTechFind=TechFindFrame(self)
        self.frmTechFind.Show(True)
    
    def OnNavNext(self,event):
        if self.page<self.pagecount:
            self.page+=1
        self.LoadData()
    
    def OnNavPrev(self,event):
        if self.page>1:
            self.page-=1
        self.LoadData()
    
    def OnHelpAbout(self,event):
        self.frmAbout=AboutFrame()
        self.frmAbout.Show(True)
 
    def OnDestroy(self,event):
        self.Destroy()
   

if __name__=="__main__":
    app=TechLogApp()
    app.MainLoop()